package com.cpi.cornpp.versionsvalues.services;

import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.versionsvalues.converter.VersionsValuesConverter;
import com.cpi.cornpp.versionsvalues.dao.VersionsValuesDAO;
import com.cpi.cornpp.versionsvalues.dto.VersionsValuesDTO;
import com.cpi.cornpp.versionsvalues.exception.VersionsValuesException;
import com.cpi.cornpp.versionsvalues.view.VersionsValuesView;
/**
 * Versions Values Service
 * @author Edgar Joao
 *
 */
public class VersionsValuesService {
	/**
	 * Get Versions Values List
	 * @param versionId
	 * @return
	 * @throws VersionsValuesException
	 */
	public List getVersionsValuesList(int versionId) throws VersionsValuesException{
		VersionsValuesDAO dao = null;
		VersionsValuesConverter converter = null;
		List versionsValuesList = null;
		try{
			dao = new VersionsValuesDAO();
			converter = new VersionsValuesConverter();
			versionsValuesList = converter.convertDTOsToViews(dao.list(versionId));			
		} catch (VersionsValuesException versionsValuesException) {
			throw versionsValuesException;
		} catch (Exception e){
			VersionsValuesException versionsValuesException = null;
			e.printStackTrace(System.out);
			versionsValuesException = new VersionsValuesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw versionsValuesException;
		}
		return versionsValuesList;
	}
	/**
	 * Save
	 * @param versionId
	 * @param versionsValuesList
	 * @throws VersionsValuesException
	 */
	public void save(int versionId, List versionsValuesList) throws VersionsValuesException{
		Iterator iterator = null;
		VersionsValuesDAO dao = null;
		VersionsValuesConverter converter = null;
		VersionsValuesView view = null;
		VersionsValuesDTO dto = null;
		try{
			dao = new VersionsValuesDAO();
			converter = new VersionsValuesConverter();			
			iterator = versionsValuesList.iterator();
			while(iterator.hasNext()){
				view = (VersionsValuesView) iterator.next();
				dto = converter.convertVersionsValuesViewToDto(view);
				dao.save(versionId, dto);
			}					
		} catch (VersionsValuesException versionsValuesException) {
			throw versionsValuesException;
		} catch (Exception e){
			VersionsValuesException versionsValuesException = null;
			e.printStackTrace(System.out);
			versionsValuesException = new VersionsValuesException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_LISTS);
			throw versionsValuesException;
		}
	}
}
